package com.boc.langxiang.presenter.me;

import android.os.SystemClock;
import android.widget.PopupWindow;

import com.boc.langxiang.contract.me.OrdersAllFragmentContract;
import com.boc.langxiang.entity.request.CancelOrdersRequest;
import com.boc.langxiang.entity.request.ConfirmReceiptRequest;
import com.boc.langxiang.entity.request.OrdersAllRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrdersAllContent;
import com.boc.langxiang.entity.response.ProductListEntity;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;
import com.boc.langxiang.ui.adapter.me.section.OrdersSection;

import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by admin on 2016/6/24.
 */
public class OrdersAllFragmentPresenter implements OrdersAllFragmentContract.Presenter {

    private final ApiService mService;
    private PopupWindow mCancelOrders;
    private OrdersAllFragmentContract.View mView;

    private long currentCancelTime;
    private long lastCancelTime = 0l;

    private List<OrdersSection> mExecuteList;

    public OrdersAllFragmentPresenter(OrdersAllFragmentContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }


    public void createCancelPopupWindow() {
        currentCancelTime = SystemClock.uptimeMillis();
        if (currentCancelTime - lastCancelTime > 200l) {

        }
    }


    @Override
    public void allorders(String page) {
        OrdersAllRequest request = new OrdersAllRequest();
        request.page = page;
        Observable<BaseResponse<OrdersAllContent>> allOrders = mService.allorders(request.params());
        allOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<OrdersAllContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<OrdersAllContent> response) {
                        OrdersAllContent content = response.getContent();
                        if (content == null) {
                            mView.getAllOrders(null);
                        } else {
                            List<ProductListEntity> list = content.getList();
                            mView.getAllOrders(list);
                        }
                    }
                });
    }

    /**
     * 取消订单
     *
     * @param id
     */
    @Override
    public void cancelOrders(String id) {
        mView.showLoading();
        CancelOrdersRequest cancelOrdersRequest = new CancelOrdersRequest();
        cancelOrdersRequest.id = id;
        Observable<BaseResponse> cancelOrders = mService.cancelOrders(cancelOrdersRequest.params());
        cancelOrders.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.cancelOrdersSuccess();
                    }
                });
    }

    @Override
    public void confirmReceipt(String id) {
        mView.showLoading();
        ConfirmReceiptRequest request = new ConfirmReceiptRequest();
        request.id = id;
        Observable<BaseResponse> confirmReceipt = mService.confirmReceipt(request.params());
        confirmReceipt.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.conFirmReceipt(response);
                    }
                });
    }
}
